﻿using Plossum.CommandLine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClrExporter
{
    [CommandLineManager(ApplicationName = "CLR Exporter", Copyright = "Copyright (c) Selman AY")]
    [CommandLineOptionGroup("Commands", Name = "Commands", Require = OptionGroupRequirement.ExactlyOne)]
    [CommandLineOptionGroup("Connectivity", Name = "Connectivity")]
    class Options
    {
        [CommandLineOption(Description = "Displays this help text", Aliases = "h")]
        public bool Help = false;

        [CommandLineOption(Description = "Specifies Name of the Server", MinOccurs = 1, Aliases="s", GroupId="Connectivity")]
        public string ServerName
        {
            get { return _serverName; }
            set
            {
                if (String.IsNullOrEmpty(value))
                    throw new InvalidOptionValueException("The ServerName must not be empty", false);

                _serverName = value;
            }
        }

        private string _serverName;

        [CommandLineOption(Description = "Specifies Name of the Database", MinOccurs = 1, Aliases = "d", GroupId = "Connectivity")]
        public string DatabaseName
        {
            get { return _databaseName; }
            set
            {
                if (String.IsNullOrEmpty(value))
                    throw new InvalidOptionValueException("The DatabaseName must not be empty", false);

                _databaseName = value;
            }
        }

        private string _databaseName;

        [CommandLineOption(Description = "Specifies the authentication type", Aliases = "i", BoolFunction = BoolFunction.TrueIfPresent, Name = "IntegratedSecurity", Prohibits = "UserName,Password", GroupId = "Connectivity")]
        public bool IntegratedSecurity { get; set; }

        [CommandLineOption(Description = "Specifies the User name for Sql Authentication", Aliases = "u", Name = "UserName", GroupId = "Connectivity")]
        public string UserName { get; set; }

        [CommandLineOption(Description = "Specifies the password for Sql Authentication", Aliases = "p", Name = "Password", GroupId = "Connectivity")]
        public string Password { get; set; }

        [CommandLineOption(Description = "Lists all assemblies in Database", BoolFunction = BoolFunction.TrueIfPresent, Aliases = "l", Name="List", GroupId = "Commands")]
        public bool List { get; set; }

        [CommandLineOption(Description = "Exports DLL", BoolFunction = BoolFunction.TrueIfPresent, Aliases = "e", Name="Export", GroupId = "Commands")]
        public bool Export { get; set; }

        [CommandLineOption(Description = "ID of the assembly to export", Aliases = "id", Name = "AssemblyId")]
        public int AssemblyId { get; set; }

        [CommandLineOption(Description = "The folder to export assemblies", Aliases = "o", Name = "Folder")]
        public string OutPutFolder 
        {
            get 
            {
                if (String.IsNullOrEmpty(this._outputFolder))
                {
                    this._outputFolder = AppDomain.CurrentDomain.BaseDirectory;
                }
                return this._outputFolder; 
            }
            set 
            {
                this._outputFolder = value;
                if (String.IsNullOrEmpty(this._outputFolder))
                {
                    this._outputFolder = AppDomain.CurrentDomain.BaseDirectory;
                }

                if (!System.IO.Directory.Exists(this._outputFolder))
                {
                    throw new InvalidOptionValueException("The output forlder should be exist.", false);
                }
            }
        }

        private string _outputFolder;
    }
}
